package com.devicemantra.coordinate.ds.tests;
import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.util.CXMLBuilder;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class GetMessagesForFrame extends HttpServlet 
{
	HttpServletRequest mReq;
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		mReq = req;
		resp.setContentType("text/xml");
		System.out.println("/ProcessMessage called now ");
		String lCFrameId = req.getParameter("cframe_id");
		String lStatus = req.getParameter("status");
		System.out.println("FRAME id is " + lCFrameId);
		System.out.println("Status  is " + lStatus);
		boolean lGiveReadMessages = false;
		if( lStatus.equals("read") )
		{
			lGiveReadMessages = true;
		}
		else if( lStatus.equals("unread") || (lStatus == null))
		{
			lGiveReadMessages = false;
		}else
		{
			return;
		}
		
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    List<CMessage1> lCMessageList = null;
	    javax.jdo.Query query = pm.newQuery(CMessage1.class, "mDestinationLocationId == lCFrameId && mIsRead == lGiveReadMessages");
	    //javax.jdo.Query query = pm.newQuery(CMessage.class, "mDestinationFrameId == lCFrameId && mIsRead == lGiveReadMessages");
		/*query.setFilter("mDestinationFrameId == lCFrameId");
		query.setFilter("mIsRead == lIsTrue"); */
		query.declareParameters("String lCFrameId, boolean lGiveReadMessages");
		lCMessageList = (List<CMessage1>) query.execute(lCFrameId,lGiveReadMessages);
		if( lGiveReadMessages == false)
		{
			markAsRead(lCMessageList);
		}
		System.out.println("Number of messages found  for " + lCFrameId + " is "  +  lCMessageList.size());
		pm.makePersistentAll(lCMessageList);
		pm.close();
		System.out.println("XML :" + getInfoAsXml(lCMessageList));
		resp.getOutputStream().println(getInfoAsXml(lCMessageList));
	}
	
	public void markAsRead(List<CMessage1> lMessageList)
	{
		for(int lCount=0; lCount < lMessageList.size(); lCount++ )
		{
			CMessage1 lMessage = lMessageList.get(lCount);
			lMessage.markAsRead();
		}
	}
	
	public String getInfoAsXml(List<CMessage1> pMessageList)
	{
		if(pMessageList.size() == 0)
		{
			return "<xml> No messages found </xml>";
			//return "<xml> " + mReq.getRequestURI() + mReq.getParameterMap().toString() +  "</xml>";
		}
		CXMLBuilder lXmlBuilder = new CXMLBuilder();
		
		/* First task is to get the url for the department logo */
		//Department lDept = DepartmentFactory.getDepartment(pFrame.getOwningDepartment());
		
		/* Start building the xml */
		lXmlBuilder.AddOpeningTag("xml");
		for(int lCounter = 0; lCounter < pMessageList.size(); lCounter++)
		{
			CMessage1 lMessage = pMessageList.get(lCounter);
			lXmlBuilder.AddOpeningTag("CMessageInfo");
			lXmlBuilder.AddNode("CFrameId", lMessage.getLocationId());
			lXmlBuilder.AddNode("CMessageId", lMessage.getMessageId());
			lXmlBuilder.AddNode("CMessage", lMessage.getMessage());
			lXmlBuilder.AddNode("CDate",lMessage.getDate().toString());
			lXmlBuilder.AddClosingTag("CMessageInfo");
		}
		
		lXmlBuilder.AddClosingTag("xml");
		return lXmlBuilder.getXml();
	}
}
